package dicky.fortunetelling;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

public class namefortuneTab extends TabActivity implements OnTabChangeListener {
	
	private String tag=namefortuneTab.class.getSimpleName();
	private TabHost myTabhost;
	protected int myMenuSettingTag=0;
	protected Menu myMenu;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.namefortunetab);  
		myTabhost = (TabHost) findViewById(android.R.id.tabhost);  
		
		Intent intent1=new Intent(this,namefortuneForTab.class);
		intent1.putExtra("type", 1);
		myTabhost.addTab(myTabhost.newTabSpec("1")
						.setIndicator("单姓单名",null)
						.setContent(intent1));
		
		Intent intent2=new Intent(this,namefortuneForTab.class);
		intent2.putExtra("type", 2);
		myTabhost.addTab(myTabhost.newTabSpec("2")
						.setIndicator("单姓复名",null)
						.setContent(intent2));

		Intent intent3=new Intent(this,namefortuneForTab.class);
		intent3.putExtra("type", 3);
		myTabhost.addTab(myTabhost.newTabSpec("3")
						.setIndicator("复姓单名",null)
						.setContent(intent3));

		Intent intent4=new Intent(this,namefortuneForTab.class);
		intent4.putExtra("type", 4);
		myTabhost.addTab(myTabhost.newTabSpec("4")
						.setIndicator("复姓复名",null)
						.setContent(intent4));
		
		TabWidget tabWidget=this.getTabWidget();
        for (int i = 0; i < tabWidget.getChildCount(); i++) {
            tabWidget.getChildAt(i).getLayoutParams().height = 50;
            TextView tv=(TextView)tabWidget.getChildAt(i).findViewById(android.R.id.title);
            tv.setTextSize(16);//设置字体的大小；
            
        }
        
		myTabhost.setOnTabChangedListener(this);
	}
	
	@Override
	public void onTabChanged(String tagString) {
		if (tagString.equals("1")) {
			myMenuSettingTag = 1;
		}
		if (tagString.equals("2")) {
			myMenuSettingTag = 2;
		}
		if (tagString.equals("3")) {
			myMenuSettingTag = 3;
		}
		if (tagString.equals("4")) {
			myMenuSettingTag = 4;
		}
	}
	
	private GestureDetector detector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
		 
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if ((e2.getRawX() - e1.getRawX()) > 80) {
                		showPre();
                        return true;
                }

                if ((e1.getRawX() - e2.getRawX()) > 80) {
                	showNext();
                        return true;
                }
                return super.onFling(e1, e2, velocityX, velocityY);
        }

	});

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		detector.onTouchEvent(event);
		return super.onTouchEvent(event);
	}

	/**
	 * 当前页面索引
	 */
	int i = 0;

	/**
	 * 显示下一个页面
	 */
	protected void showNext() {
		// 三元表达式控制3个页面的循环.
		if (i<3){
			myTabhost.setCurrentTab(i = ++i);
		}
//		myTabhost.setCurrentTab(i = i == 3 ? i = 0 : ++i);
	}

	/**
	 * 显示前一个页面
	 */
	protected void showPre() {
		// 三元表达式控制3个页面的循环.
		if (i>0){
			myTabhost.setCurrentTab(i = --i);
		}
//		myTabhost.setCurrentTab(i = i == 0 ? i = 3 : --i);

	}
}
